/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */
 
#include "db_sqlite.h"
#include <string.h>

/*void save_state(void)
{
	int db_ret;
	char *err, *sql_st=NULL, *sql;

	//clear up the state table
	sql = "DELETE FROM state;";
	db_ret = sqlite3_exec(songs, sql, NULL, NULL, &err);
	if (db_ret != SQLITE_OK) {
	  if (err != NULL) {
		  fprintf(stderr, "SQL error: %s\n", err);
		  sqlite3_free(err);
	  }
	}
	if (play_state == PAUSE) seek = pos_time;
	else seek = 0;	
	//save cur_playlist, song_index, vol
	sql_st = sqlite3_mprintf("INSERT INTO state(song_no, volume, seek, dur) VALUES('%q', %d, %d, %d, %d);",
							song_index, vol, seek, song_dur);
	printf("%s\n", sql_st);
	db_ret = sqlite3_exec(songs, sql_st, NULL, NULL, &err);
	if (db_ret != SQLITE_OK) {
	  if (err != NULL) {
		  fprintf(stderr, "SQL error: %s\n", err);
		  sqlite3_free(err);
	  }
	}
	sqlite3_free(sql_st);
}

void restore_state(void)
{
	int db_ret, ret;
	char *sql, volume[255], db[255];
	const char  *tail;
	sqlite3_stmt *stmt;

	//set intone directory
	sprintf(intone_dir, "/usr/share/intone");
	//get the users home directory
	const char *name = "HOME";
	sprintf(home_dir, "%s", getenv(name));
	sprintf(db, "%s/.intone/intone_songs.db", home_dir);
	//connect to the db
	db_ret = sqlite3_open(db, &songs);
	
	if (db_ret == SQLITE_ERROR || db_ret == SQLITE_CANTOPEN) {
		printf("SQL error: %s\n", sqlite3_errmsg(songs));
		printf("Creating new db file\n");
		//exit(1);
		ret = system("mkdir ~/.intone");
		sqlite3_close(songs);
		db_ret = sqlite3_open(db, &songs);
	}

	//load the state data from state table
	sql = "SELECT *  FROM state";	
	db_ret = sqlite3_prepare(songs, sql, strlen(sql), &stmt, &tail);
	if(db_ret != SQLITE_OK) {
		//if (strcmp(sqlite3_errmsg(songs), "no such table: state")==0) first_run();
		printf("SQL error: %d %s\n", db_ret, sqlite3_errmsg(songs));
	}
	else {
		//set up environment variables
		while((db_ret = sqlite3_step(stmt)) == SQLITE_ROW) {
			sprintf(cur_playlist, "%s", sqlite3_column_text(stmt, 1));
			song_index = sqlite3_column_int(stmt, 2);
			vol = sqlite3_column_int(stmt, 3);
			seek = sqlite3_column_int(stmt, 4);
			song_dur = sqlite3_column_int(stmt, 5);
		}
	}
	sqlite3_finalize(stmt);
	//set up no_songs
	sql = sqlite3_mprintf("SELECT COUNT(key) FROM '%q';", cur_playlist);
	db_ret = sqlite3_prepare(songs, sql, strlen(sql), &stmt, &tail);
	if(db_ret != SQLITE_OK) {
		printf("SQL error: %s\n", sqlite3_errmsg(songs));
	}
	db_ret = sqlite3_step(stmt);
	if (db_ret == SQLITE_ROW) no_songs = sqlite3_column_int(stmt, 0);
	else {
		printf("SQL error: %s\n", sqlite3_errmsg(songs));
	}
	sqlite3_free(sql);
	sqlite3_finalize(stmt);
	if(song_index > no_songs - 1) song_index = no_songs - 1;
	//set the volume
	sprintf(volume, "amixer -c 0 set PCM %d%s", vol, "\%\n");
	db_ret = system(volume);
}

void begin_transaction(void)
{
	sqlite3_exec(songs, "BEGIN", 0, 0, 0);
}

void commit_transaction(void)
{
	sqlite3_exec(songs, "COMMIT", 0, 0, 0);
}

*/





